package com.love.testnetwork;

import android.util.Log;

import java.io.IOException;
import java.util.Locale;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zf on 2016/11/26.
 */

public class LogInterceptor implements Interceptor {
    private static final String TAG = "LogInterceptor";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        Log.d(TAG, "intercept() returned: " + request.toString());

        long l1 = System.nanoTime();

        Response response = chain.proceed(request);

        long l2 = System.nanoTime();

        Log.v(TAG, String.format(Locale.getDefault(), "Received response for %s in %.1fms%n%s",
                response.request().url(), (l2 - l1) / 1e6d, response.headers()));

        MediaType mediaType = response.body().contentType();

        String string = response.body().string();
        Log.i(TAG, "response body: " + string);

        return response.newBuilder()
                .body(okhttp3.ResponseBody.create(mediaType, string))
                .build();
    }
}
